import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xingsu/page/publisher/detail_page.dart';
import 'package:xingsu/page/publisher/new_more_page.dart';
import 'package:xingsu/page/publisher/viewModel/task_search_viewmodel.dart';
import 'package:xingsu/util/image_util.dart';
import 'package:xingsu/widget/back_widget.dart';
import 'package:xingsu/widget/search_widget.dart';

import '../../res/colors.dart';

class TaskSearchPage extends StatelessWidget {
  final TaskSearchViewModel _viewModel = Get.put(TaskSearchViewModel());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 0,
      ),
      body: Column(
        children: [
          _topWidget(),
          Expanded(
              child: Obx(() => ListView(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    children: _viewModel.list
                        .map((element) => _itemWidget(element))
                        .toList(),
                  )))
        ],
      ),
    );
  }

  Widget _topWidget() {
    return Row(
      children: [
        const SizedBox(
          width: 10,
        ),
        IconButton(
            onPressed: () {
              Get.back();
            },
            icon: const Icon(
              Icons.arrow_back_ios,
              size: 24,
            )),
        const SizedBox(
          width: 8,
        ),
        Expanded(
            child: SearchWidget(
                placeholder: "搜索任务",
                controller: _viewModel.controller,
                onSubmitted: (str) {
                  _viewModel.search();
                })),
        const SizedBox(
          width: 10,
        ),
        InkWell(
          onTap: () {
            _viewModel.search();
          },
          child: const Text(
            "搜索",
            style: TextStyle(color: Colours.app_main, fontSize: 14),
          ),
        ),
        const SizedBox(
          width: 10,
        ),
      ],
    );
  }

  Widget _itemWidget(Map parent) {
    Map detail_info = parent["detail_info"] ?? {};
    Map data = detail_info["data"] ?? {};
    Map map = data["task_info"];

    List tags = map["GameTags"] ?? [];
    Map taskStat = map["TaskStat"] ?? {};
    return InkWell(
      onTap: () {
        Get.to(() => DetailPage(parent));
      },
      child: Container(
        height: 125,
        alignment: Alignment.centerLeft,
        margin: const EdgeInsets.only(top: 15),
        padding: const EdgeInsets.symmetric(horizontal: 15),
        decoration: BoxDecoration(
            color: const Color(0xFF22262F),
            borderRadius: BorderRadius.circular(5)),
        child: Row(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: loadNetworkImage(map["GameIcon"] ?? "",
                  width: 80, height: 80),
            ),
            const SizedBox(
              width: 15,
            ),
            Expanded(
                child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  map["TaskName"] ?? "",
                  maxLines: 1,
                  style: const TextStyle(color: Colors.white, fontSize: 16),
                ),
                Text(
                  "${taskStat["UserCount"] ?? 0}人投稿",
                  maxLines: 1,
                  style: const TextStyle(color: Colors.grey, fontSize: 12),
                ),
                Row(
                  children: tags.map((e) => tagItemWidget(e)).toList(),
                ),
                Text.rich(TextSpan(children: [
                  const TextSpan(
                      text: "当前最高收益:",
                      style: TextStyle(fontSize: 12, color: Colors.grey)),
                  TextSpan(
                      text: "￥${taskStat["RecentMaxTaskIncome"] ?? 0}",
                      style:
                          const TextStyle(color: Colors.white, fontSize: 15)),
                ])),
              ],
            ))
          ],
        ),
      ),
    );
  }
}
